// 创建axios实例
import { getUserTempId } from '@/utils'
import axios from 'axios'
import nprogress from 'nprogress'

const sphRequest = axios.create({
  baseURL: '/api',
})
// 请求拦截
sphRequest.interceptors.request.use((config) => {
  // 发送请求前启动
  nprogress.start()
  //为请求头增加userTempId
  config.headers.userTempId = getUserTempId()
  return config
})
// 响应拦截
sphRequest.interceptors.response.use(
  (res) => {
    nprogress.done() // 结束请求后关闭
    return res.data
  },
  (err) => {
    nprogress.done() // 请求异常关闭
    alert(err.message)
    // 中断Promise链条
    return new Promise(() => {})
  }
)
export default sphRequest
